class NotesController < ApplicationController

  layout "print"

  def index
  @user = User.find(session[:user_id])
  @resource = Resource.find(params[:resource_id])
  if((not @user.canSeeOthersNotes) or params[:my_notes]) then
  	@notes = Note.find(:all , :conditions => "resource_id = #{params[:resource_id]} AND user_id = #{session[:user_id]}")
  else
  	if(params[:pretty_print_notes]) then
#   	note_grade = NoteGrade.find(:first, :conditions => "user_id = #{@user.id} AND note_id = "
  	@notes = Note.find(:all , :conditions => "resource_id = #{params[:resource_id]} AND ((visibility = 0 ) OR user_id = #{session[:user_id]})")
  	else
    @notes = Note.find(:all , :conditions => "resource_id = #{params[:resource_id]} AND (visibility = 0 OR user_id = #{session[:user_id]})")
    end
  end
      respond_to do |format|
      format.html # index.html.erb
      format.xml  { render :xml => @note }
    end
  end

def delitableNote
	line = ""
	line = "<%= link_to 'Destroy', note , :confirm => 'Are you sure?', :method => :delete %>" if((params[:note]).user_id == session[:user_id]) 
	render :inline => line
end
  def show
  end

 def new
 	@user = User.find(params[:user_id])
 	@resource = Resource.find(params[:resource_id])
    @note = Note.new

    respond_to do |format|
      format.html # new.html.erb
      format.xml  { render :xml => @note }
    end
    
  end

  # GET /tags/1/edit
  def edit
    @note = Note.find(params[:id])
  end

  # POST /tags
  # POST /tags.xml
  def create
    @note = Note.new
	@note.user_id = params[:user_id]
	@note.resource_id = params[:resource_id]
	@note.content = (params[:note])[:content]
	vis = 0
	vis = 1 if ((params[:note])[:visibility] == "invisible")
	@note.visibility = vis
	@note.save
 	redirect_to :action => 'show' , :controller => 'resources' , :id => params[:resource_id]
#	render :text => " "
#     respond_to do |format|
#       if @note.save
#         flash[:notice] = 'Tag was successfully created.'
#         format.html { redirect_to(@note) }
#         format.xml  { render :xml => @note, :status => :created, :location => @note }
#       else
#         format.html { render :action => "new" }
#         format.xml  { render :xml => @note.errors, :status => :unprocessable_entity }
#       end
#     end
  end
  
  
    def update
    @note = Note.find(params[:id])
    @note.update_attributes(params[:note])
    vis = 0
	vis = 1 if ((params[:note])[:visibility] == "invisible")
	@note.visibility = vis
	@note.save
# 	redirect_to :back
	redirect_to :action => 'show' , :controller => 'resources' , :id => @note.resource_id
  end
  
    def destroy
    @note = Note.find(params[:id])
    @note.destroy
	redirect_to :back
	end
  
  def print()
  		@resource = Resource.find(params[:resource_id])
  	   @notes = Note.find(:all , :conditions => "resource_id = #{params[:resource_id]} AND (visibility = 0 OR user_id = #{session[:user_id]})")
      respond_to do |format|
      format.html # index.html.erb
      format.xml  { render :xml => @note }
    end
  end
  
  def grade_with
  	val = params[:val].to_f
    @note = Note.find(params[:id]) #id
    @user = User.find(session[:user_id])
    @note.grade(val,@user.id)
#     render :text => "voted"
#     redirect_to :action => 'show' , :controller => 'resources' , :id => @note.resource_id
#     redirect_to :back
	render :partial => 'note_graded'
  end
end
